home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c14 / Sharing1.java < prev    next >
Encoding:
Java Source  |  2000-05-25  |  5.8 KB  |  181 lines

  1. //: Sharing1.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // Problems with resource sharing while threading
  50. import java.awt.*;
  51. import java.awt.event.*;
  52. import java.applet.*;
  53.  
  54. class TwoCounter extends Thread {
  55.   private boolean started = false;
  56.   private TextField 
  57.     t1 = new TextField(5),
  58.     t2 = new TextField(5);
  59.   private Label l = 
  60.     new Label("count1 == count2");
  61.   private int count1 = 0, count2 = 0;
  62.   // Add the display components as a panel
  63.   // to the given container:
  64.   public TwoCounter(Container c) {
  65.     Panel p = new Panel();
  66.     p.add(t1);
  67.     p.add(t2);
  68.     p.add(l);
  69.     c.add(p);
  70.   }
  71.   public void start() {
  72.     if(!started) {
  73.       started = true;
  74.       super.start();
  75.     }
  76.   }
  77.   public void run() {
  78.     while (true) {
  79.       t1.setText(Integer.toString(count1++));
  80.       t2.setText(Integer.toString(count2++));
  81.       try {
  82.         sleep(500);
  83.       } catch (InterruptedException e){}
  84.     }
  85.   }
  86.   public void synchTest() {
  87.     Sharing1.incrementAccess();
  88.     if(count1 != count2)
  89.       l.setText("Unsynched");
  90.   }
  91. }
  92.  
  93. class Watcher extends Thread {
  94.   private Sharing1 p;
  95.   public Watcher(Sharing1 p) { 
  96.     this.p = p;
  97.     start();
  98.   }
  99.   public void run() {
  100.     while(true) {
  101.       for(int i = 0; i < p.s.length; i++)
  102.         p.s[i].synchTest();
  103.       try {
  104.         sleep(500);
  105.       } catch (InterruptedException e){}
  106.     }
  107.   }
  108. }
  109.  
  110. public class Sharing1 extends Applet {
  111.   TwoCounter[] s;
  112.   private static int accessCount = 0;
  113.   private static TextField aCount = 
  114.     new TextField("0", 10);
  115.   public static void incrementAccess() {
  116.     accessCount++;
  117.     aCount.setText(Integer.toString(accessCount));
  118.   }
  119.   private Button 
  120.     start = new Button("Start"),
  121.     observer = new Button("Observe");
  122.   private boolean isApplet = true;
  123.   private int numCounters = 0;
  124.   private int numObservers = 0;
  125.   public void init() {
  126.     if(isApplet) {
  127.       numCounters = 
  128.         Integer.parseInt(getParameter("size"));
  129.       numObservers = 
  130.         Integer.parseInt(
  131.           getParameter("observers"));
  132.     }
  133.     s = new TwoCounter[numCounters];
  134.     for(int i = 0; i < s.length; i++)
  135.       s[i] = new TwoCounter(this);
  136.     Panel p = new Panel();
  137.     start.addActionListener(new StartL());
  138.     p.add(start);
  139.     observer.addActionListener(new ObserverL());
  140.     p.add(observer);
  141.     p.add(new Label("Access Count"));
  142.     p.add(aCount);
  143.     add(p);
  144.   }
  145.   class StartL implements ActionListener {
  146.     public void actionPerformed(ActionEvent e) {
  147.       for(int i = 0; i < s.length; i++)
  148.         s[i].start();
  149.     }
  150.   }
  151.   class ObserverL implements ActionListener {
  152.     public void actionPerformed(ActionEvent e) {
  153.       for(int i = 0; i < numObservers; i++)
  154.         new Watcher(Sharing1.this);
  155.     }
  156.   }
  157.   public static void main(String[] args) {
  158.     Sharing1 applet = new Sharing1();
  159.     // This isn't an applet, so set the flag and
  160.     // produce the parameter values from args:
  161.     applet.isApplet = false;
  162.     applet.numCounters = 
  163.       (args.length == 0 ? 5 :
  164.         Integer.parseInt(args[0]));
  165.     applet.numObservers =
  166.       (args.length < 2 ? 5 :
  167.         Integer.parseInt(args[1]));
  168.     Frame aFrame = new Frame("Sharing1");
  169.     aFrame.addWindowListener(
  170.       new WindowAdapter() {
  171.         public void windowClosing(WindowEvent e){
  172.           System.exit(0);
  173.         }
  174.       });
  175.     aFrame.add(applet, BorderLayout.CENTER);
  176.     aFrame.setSize(350, applet.numCounters *100);
  177.     applet.init();
  178.     applet.start();
  179.     aFrame.setVisible(true);
  180.   }
  181. } ///:~